//
//  getVersionViewController.m
//  JFTRequest
//
//  Created by 竣付通 on 2017/11/14.
//  Copyright © 2017年 junHaoPay. All rights reserved.
//

#import "getVersionViewController.h"
#import "JFTRequest.h"
#import "MBProgressHUD.h"
#import "HLMBProgressHUD.h"
@interface getVersionViewController ()
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;

@end

@implementation getVersionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (IBAction)getVersionInfo:(UIButton *)sender {
    if (![[JFTRequest requestManager] ConnectedToNetwork]) {
        [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:2 hudDetailText:@"请检查网络连接！"];
        return;
    }
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(concurrentQueue, ^{
        NSString *newVersion =nil;
        NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/cn/lookup?id=1274851621"];
        
        NSString *jsonResponseString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
        
        NSData *data = [jsonResponseString dataUsingEncoding:NSUTF8StringEncoding];
        
        if (!data) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:2 hudDetailText:@"网络超时，请检查您的网络！"];
            });
            
            return ;
        }else{
            NSError *error =nil;
            id json =nil;
            
            if ([NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]) {
                json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    
                });
                
            }
            
            if ([json isKindOfClass:[NSDictionary class]]) {
                NSArray *array = json[@"results"];
                for (NSDictionary *dic in array) {
                    newVersion = [dic valueForKey:@"version"];
                }
            }
        }
        if (!newVersion) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:2 hudDetailText:@"已经是最新版本"];
            });
            
            return;
        }
        //获取本地软件的版本号
        NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        //对比发现的新版本和本地的版本
        if (![newVersion isEqualToString: localVersion])
        {
            dispatch_async(dispatch_get_main_queue(), ^{
            self.versionLabel.text = newVersion;
          });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:2 hudDetailText:@"已经是最新版本"];
            });
        }
        
    });
}


@end
